/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interpreter;

import ast.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Stephen
 */
public class Assign {

    private AST t;
    private InterpreterVisitor iv;

    public Assign(AST t, InterpreterVisitor iv) {
        this.t = t;
        this.iv = iv;
    }

    public Object execute() {
        try {
            if (t.getKid(1).getClass() == Class.forName("ast.IdTree")) {
                IdTree id = (IdTree) t.getKid(1);
                String varName = id.getSymbol().toString();
                
                if (t.getKid(2).getClass() == Class.forName("ast.ObjectDeclTree")) {
                    DSSObject o = new DSSObject((ObjectDeclTree)t.getKid(2));
                    SymbolTable.Scope.addObject(varName, o);
                    return null;
                }
                
                if (t.getKid(2).getClass() == Class.forName("ast.ObjectTree")) {
                    String objName = ((IdTree)(t.getKid(2).getKid(1))).getSymbol().toString();
                    DSSObject o = (DSSObject) SymbolTable.Scope.getObject(objName);
                    ArrayList fields = o.getFields();
                    for (int i = 0; i < fields.size(); i ++) {
                        String field = ((IdTree)fields.get(i)).getSymbol().toString();
                        SymbolTable.Scope.addSymbol(varName + "." + field, "test", "string");
                    }
                    SymbolTable.Scope.addSymbol(varName, o, "object");
                    return null;
                }
                
                Object value = t.getKid(2).accept(iv);
                if (value.getClass() == Class.forName("java.util.ArrayList")) {
                    SymbolTable.Scope.addSymbol(varName, value, "list");
                } else if (value.getClass() == Class.forName("java.lang.Boolean")) {
                    SymbolTable.Scope.addSymbol(varName, value, "boolean");
                } else if (value.getClass() == Class.forName("java.util.Date")) {
                    SymbolTable.Scope.addSymbol(varName, value, "date");
                } else if (value.getClass() == Class.forName("Interpreter.Observation")) {
                    SymbolTable.Scope.addSymbol(varName, value, "observation");
                } else if (value.getClass() == Class.forName("Interpreter.DSSObject")) {
                    String objName = ((IdTree)(t.getKid(2))).getSymbol().toString();
                    DSSObject o = (DSSObject) SymbolTable.Scope.getVarValue(objName);
                    ArrayList fields = ((DSSObject)o).getFields();
                    for (int i = 0; i < fields.size(); i++) {
                        String field = ((IdTree)fields.get(i)).getSymbol().toString();
                        String name = (objName + "." + field);
                        String type = SymbolTable.Scope.getVarType(name);
                        SymbolTable.Scope.addSymbol(varName + "." + field, SymbolTable.Scope.getVarValue(name), type);
                    }
                    SymbolTable.Scope.addSymbol(varName, value, "object");
                } else {
                    String type = InterpreterVisitor.parseType((String) value);
                    SymbolTable.Scope.addSymbol(varName, value, type);
                }

            } else if (t.getKid(1).getClass() == Class.forName("ast.FieldRefTree")) {
                FieldRefTree id = (FieldRefTree) t.getKid(1);
                String obj = ((IdTree) id.getKid(1)).getSymbol().toString();
                String field = ((IdTree) id.getKid(2)).getSymbol().toString();
                Object value = t.getKid(2).accept(iv);
                if (value.getClass() == Class.forName("java.util.ArrayList")) {
                    SymbolTable.Scope.addSymbol(obj + "." + field, value, "list");
                } else if (value.getClass() == Class.forName("java.lang.Boolean")) {
                    SymbolTable.Scope.addSymbol(obj + "." + field, value, "boolean");
                } else if (value.getClass() == Class.forName("java.util.Date")) {
                    SymbolTable.Scope.addSymbol(obj + "." + field, value, "date");
                } else if (value.getClass() == Class.forName("Interpreter.Observation")) {
                    SymbolTable.Scope.addSymbol(obj + "." + field, value, "observation");
                } else {
                    String type = InterpreterVisitor.parseType((String) value);
                    SymbolTable.Scope.addSymbol(obj + "." + field, value, type);
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(InterpreterVisitor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
